package com.zzyl.controller;

import cn.hutool.json.JSONObject;
import com.aliyun.iot20180120.models.QueryDevicePropertyStatusRequest;
import com.aliyun.iot20180120.models.QueryDevicePropertyStatusResponseBody;
import com.aliyun.iot20180120.models.QueryThingModelPublishedRequest;
import com.aliyun.iot20180120.models.QueryThingModelPublishedResponseBody;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.dto.DevicePageQueryDto;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceDataVo;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.ProductVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description ProductController
 * @Author Hua Caoxing
 * @Data 2025-09-10
 */
@RestController
@RequestMapping("/iot")
public class ProductController extends BaseController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 从物联网平台同步餐品列表
     */
    @PostMapping("/syncProductList")
    public ResponseResult syncProductList(){
        deviceService.syncProductList();
        return success();
    }
    /**
     * 获取产品列表
     */
    @GetMapping("/allProduct")
    public ResponseResult<List<ProductVo>> allProduct(){
       List<ProductVo> productVoList =  deviceService.allProduct();
        return success(productVoList);
    }
    /**
     * 注册设备
     */
    @PostMapping("/RegisterDevice")
    public ResponseResult registerDevice(@RequestBody DeviceDto deviceDto){

        deviceService.registerDevice(deviceDto);

        return success();
    }
    /**
     * 设备列表
     */
    @GetMapping("/pageQueryDevice")
    public  ResponseResult<PageResponse<DeviceVo>> pageQueryDevice(@RequestParam Integer pageNum,
                                                                   @RequestParam Integer pageSize,
                                                                   @RequestParam(required = false) String deviceName,
                                                                   @RequestParam(required = false) String productKey,
                                                                   @RequestParam(required = false) Integer locationType){
        DevicePageQueryDto devicePageQueryDto = new DevicePageQueryDto();
        devicePageQueryDto.setDeviceName(deviceName);
        devicePageQueryDto.setProductKey(productKey);
        devicePageQueryDto.setLocationType(locationType);
        devicePageQueryDto.setPageNum(pageNum);
        devicePageQueryDto.setPageSize(pageSize);
        PageResponse<DeviceVo> pageResponse = deviceService.pageQueryDevice(devicePageQueryDto);
        return success(pageResponse);
    }

    /**
     * 查询设备详细信息
     */
    @PostMapping("/QueryDeviceDetail")
    public ResponseResult<DeviceVo> QueryDeviceDetail(@RequestBody DeviceDto deviceDto){
        DeviceVo deviceVo = deviceService.QueryDeviceDetail(deviceDto);

        return success(deviceVo);
    }

    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     * @param productVo
     * @return
     */
    @PostMapping("/QueryThingModelPublished")
    public ResponseResult<QueryThingModelPublishedResponseBody.QueryThingModelPublishedResponseBodyData> QueryThingModelPublished(@RequestBody QueryThingModelPublishedRequest request){
         return deviceService.QueryThingModelPublished(request);

    }

    /**
     * 查询指定设备的物模型运行状态
     */
    @PostMapping("/QueryDevicePropertyStatus")
    public ResponseResult<QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData> QueryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest queryDevicePropertyStatusRequest){
        QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData responseBodyData= deviceService.QueryDevicePropertyStatus(queryDevicePropertyStatusRequest);
        return success(responseBodyData);
    }

    /**
     *  修改设备
     */
    @PostMapping("/UpdateDevice")
    public ResponseResult UpdateDevice(@RequestBody DeviceDto deviceDto){
        deviceService.UpdateDevice(deviceDto);
        return success();
    }

    @DeleteMapping("/DeleteDevice")
    public ResponseResult DeleteDevice(@RequestBody DeviceDto deviceDto){
        deviceService.DeleteDevice(deviceDto);
        return success();

    }

}
